<template>
    <el-card style="max-width: 480px">
    <template #header>
      <div class="card-header">
        <span>用户注册</span>
      </div>
    </template>
    <el-form :model="form" label-width="auto" :rules="registerRules" ref="registerFormRef"  style="width: 300px">
    <el-form-item label="用户名" prop="userName">
      <el-input v-model="form.userName"  placeholder="请输入用户名" />
    </el-form-item>
    <el-form-item label="密码" prop="userPassword">
      <el-input v-model="form.userPassword"  type="password" show-password placeholder="请输入密码" />
    </el-form-item> 
    <el-form-item label="真实姓名" prop="realName">
      <el-input v-model="form.realName"  placeholder="请输入真实姓名"  />
    </el-form-item>
    <!-- <el-form-item label="上传文件">
      <input type="file" ref="file" @change="upload" />
    </el-form-item> -->
    <el-form-item>
      <el-button type="primary" @click="registerAction">注册</el-button>
      <el-button @click="toLogin">返回</el-button>
    </el-form-item>
  </el-form>
  </el-card>
</template>
<script>
import Login from '../api/login'
export default{
    data(){
        return{
            form:{},
            registerRules: {
                userName: [
                  { required: true, message: '请输入用户名', trigger: 'blur' }
                ],
                userPassword: [
                  { required: true, message: '请输入密码', trigger: 'blur' },
                  { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
                ],
                realName: [
                  { required: true, message: '请输入真实姓名', trigger: 'blur' }
                ],
              },
        }
    },
    methods:{
        toLogin(){
            this.$router.push({name:'login'})
        },
        registerAction(){
           this.$refs.registerFormRef.validate((valid)=>{
            if(valid){
              this.form.userType = 2
              this.form.userEnable = 1
              this.form.userRole = 'USER'
              Login.register(this.form).then(r=>{
                if(r.code==1){
                    
                    this.$router.push({name:"login"})
                }else{
                    
                }
              })
            }
          }) 
        }
    }
}
</script>